package com.jeomo.sys.starter.service.impl;

import com.jeomo.base.starter.service.impl.BaseServiceImpl;
import com.jeomo.common.core.dto.PageQueryDto;
import com.jeomo.common.core.dto.PageResponseDto;
import com.jeomo.sys.starter.entity.Dict;
import com.jeomo.sys.starter.mapper.DictMapper;
import com.jeomo.sys.starter.service.IDictService;
import org.springframework.stereotype.Service;

import javax.validation.constraints.NotBlank;
import java.util.List;

/**
 * 字典service实现类
 *
 * @author QBT
 */
@Service
public class DictServiceImpl extends BaseServiceImpl<DictMapper, Dict> implements IDictService {

    @Override
    public int remove(@NotBlank String typeCode, @NotBlank String code) {
        return this.baseMapper.remove(typeCode, code);
    }
    @Override
    public List<Dict> queryByTypeCode(@NotBlank String typeCode) {
        return this.baseMapper.queryByTypeCode(typeCode);
    }

    @Override
    public Dict queryByCode(String typeCode, String code) {
        return this.baseMapper.queryByCode(typeCode, code);
    }

    @Override
    public List<String> queryAllType() {
        return this.baseMapper.queryAllType();
    }

    /**
     * 重写保存方法
     * 先查询，如果有的话，则修改，如果没的话，则新建
     * @param entity
     * @return
     */
    @Override
    public boolean save(Dict entity) {
        Dict oldEntity = queryByCode(entity.getType(), entity.getCode());
        if(oldEntity == null) {
            return super.save(entity);
        } else {
            oldEntity.setLabel(entity.getLabel());
            oldEntity.setCode(entity.getCode());
            oldEntity.setMemo(entity.getMemo());
            oldEntity.setType(entity.getType());
            return super.updateById(oldEntity);
        }
    }

    @Override
    public PageResponseDto pageSearch(PageQueryDto dt) {
        return super.pageSearch(dt);
    }
}
